package com.teamfractal.fracdustry.client.screen.Generators;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.teamfractal.fracdustry.common.Fracdustry;
import com.teamfractal.fracdustry.common.container.Generators.FDSolarGeneratorContainer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.entity.player.Inventory;

import javax.annotation.Nonnull;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class FDSolarGeneratorScreen extends AbstractContainerScreen<FDSolarGeneratorContainer> {
    private final ResourceLocation SCREEN = new ResourceLocation(Fracdustry.MODID, "textures/gui/solar_generator/screen.png");

    public FDSolarGeneratorScreen(FDSolarGeneratorContainer container, Inventory inv, Component name) {
        super(container, inv, name);
    }

    @Override
    public void render(@Nonnull PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderTooltip(matrixStack, mouseX, mouseY);
    }

    //render labels
    @Override
    protected void renderLabels(@Nonnull PoseStack matrixStack, int mouseX, int mouseY) {
        int relX = (this.width - 180) / 2;
        int relY = (this.height - 152) / 2;
        List<FormattedCharSequence> tooltip1 = new ArrayList<>();
        List<FormattedCharSequence> tooltip2 = new ArrayList<>();
        //Energy tooltip display
        if(mouseX > relX + 66 && mouseX < relX + 112 && mouseY > relY + 61 && mouseY < relY + 66){
            tooltip1.add(new TextComponent(new TranslatableComponent("gui.fracdustry.energy").getString()+
                    menu.getCurrentEnergy().get(0)+
                    " FE").getVisualOrderText());
            renderTooltip(matrixStack,tooltip1,mouseX - relX,mouseY - relY);}
        if(mouseX > relX + 87 && mouseX < relX + 92 && mouseY > relY + 12 && mouseY < relY + 17){
            tooltip2.add(new TextComponent(MessageFormat.format(new TranslatableComponent("gui.fracdustry.solar_status").getString()
                    , menu.getLight(), 20 * Math.max(0,menu.getLight()-10),new TranslatableComponent("units.fracdustry.sec").getString())).getVisualOrderText()

            );
            renderTooltip(matrixStack,tooltip2,mouseX - relX,mouseY - relY);}
    }

    protected void renderBg(@Nonnull PoseStack matrixStack, float partialTicks, int mouseX, int mouseY) {
        RenderSystem.setShaderTexture(0, SCREEN);
        //Background
        int relX = (this.width - 180) / 2;
        int relY = (this.height - 152) / 2;
        blit(matrixStack, relX, relY, 0, 0, 180, 152, 256, 256);
        //Title
        this.font.draw(matrixStack, new TranslatableComponent("gui.fracdustry.title.solar_generator").getString() , relX + 4, relY + 4, -12829636);
        //Battery
        RenderSystem.setShaderTexture(0, SCREEN);
        double ce = menu.getCurrentEnergy().get(0);
        double me = menu.getMaxEnergy().get(0);
        int length = Math.toIntExact(Math.round((ce / me) * 45));
        blit(matrixStack, relX + 67, relY + 62, 180, 0, length,
                4,256,256);
        //Sunlight
        RenderSystem.setShaderTexture(0, SCREEN);
        int i = Math.max(menu.getLight()-10,0)/2;
        if (menu.getLight() > 10)
        {blit(matrixStack, relX + 87, relY + 12, 180 + 6 * i, 4, 6,
                6,256,256);}
    }
}
